/**
* Menu class which handles events for multiple ComplexButton instances
* @author Devon O.
*/

package src.uipackage.ui.complexbuttons {
	
	import com.onebyonedesign.ui.graphics.AccordionSection;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.utils.Dictionary;
	import com.onebyonedesign.ui.complexbuttons.events.ComplexButtonEvent;

	public class Menu extends Sprite {
		
		private var _buttons:Dictionary;
		private var _buttonArray:Array;
		
		public function Menu() {
			_buttons = new Dictionary();
			_buttonArray = new Array();
		}
		
		public function addButton(btn:*, x:Number, y:Number):void {
			if (!_buttons[btn.data]) {
				if (btn is ComplexAccordionButton) {
					_buttonArray.push(btn.buttonInstance.content);
				} else {
					_buttonArray.push(btn.buttonInstance);
				}
				_buttons[btn.data] = btn;
				btn.x = x;
				btn.y = y;
				btn.addEventListener(ComplexButtonEvent.RELEASE, btnRelease);
				btn.addEventListener(ComplexButtonEvent.PRESS, redispatch);
				btn.addEventListener(ComplexButtonEvent.RELEASE_OUTSIDE, redispatch);
				btn.addEventListener(ComplexButtonEvent.DRAG_OUT, redispatch);
				btn.addEventListener(ComplexButtonEvent.DRAG_OVER, redispatch);
				btn.addEventListener(ComplexButtonEvent.ROLL_OUT, redispatch);
				btn.addEventListener(ComplexButtonEvent.ROLL_OVER, redispatch);
				addChild(btn);
			} else {
				throw new Error("Menu Error: attempted to add multiple buttons with duplicate data properties.");
			}
		}
		
		public function removeButton(dat:String):void {
			if (_buttons[dat]) {
				var btn:* = _buttons[dat];
				btn.removeEventListener(ComplexButtonEvent.RELEASE, btnRelease);
				btn.removeEventListener(ComplexButtonEvent.PRESS, redispatch);
				btn.removeEventListener(ComplexButtonEvent.RELEASE_OUTSIDE, redispatch);
				btn.removeEventListener(ComplexButtonEvent.DRAG_OUT, redispatch);
				btn.removeEventListener(ComplexButtonEvent.DRAG_OVER, redispatch);
				btn.removeEventListener(ComplexButtonEvent.ROLL_OUT, redispatch);
				btn.removeEventListener(ComplexButtonEvent.ROLL_OVER, redispatch);
				removeChild(btn);
				delete _buttons[dat];
			}
		}
		
		
		private function btnRelease(cbe:ComplexButtonEvent):void {
			redispatch(cbe);
			for (var btn:* in _buttons) if (_buttons[btn].selected) _buttons[btn].reset();
		}
		
		private function redispatch(cbe:ComplexButtonEvent):void {
			cbe.stopPropagation();
			dispatchEvent(new ComplexButtonEvent(cbe.type, cbe.data));
		}
		
		/**
		* returns the data property of the currently selected button. If no button is selected, returns null
		*/
		public function get selected():String {
			for (var btn:* in _buttons) if (_buttons[btn].selected) return _buttons[btn].data;
			return null;
		}
		/**
		* sets the button associated with the passed data string as selected
		*/
		public function set selected(btnData:String):void {
			if(_buttons[btnData]) {
				_buttons[btnData].selected = true;
			} else {
				throw new Error("Menu Error: attempted to select non existent button");
			}
		}
		
		public function getItemAt(index:int):* {
			return _buttonArray[index];
		}
	}
	
}
